/**
 * 全局变量
 */
var selectUserId = '';
$(function(){
    /*
    * 加载列表数据
    * */
    $('#listGrid').datagrid({
        url:ROOT_PATH+'/risk/region/point/queryListByParam.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},
            {field:'name',title:'风险点名称',width:10},
            {field:'regionName',title:'风险区域名称(简称)',width:10},
            /*{field:'typeName',title:'风险区域类别',width:10},
            {field:'deptName',title:'风险区域所在部门',width:10},*/
            {field:'sort',title:'排序',width:10},
            {field:'addUserName',title:'登记人员',width:10},
            {field:'addDeptName',title:'登记部门',width:10},
            {field:'addTime',title:'登记时间',width:10},
            {field:'remarks',title:'备注',width:10}
        ]],
        toolbar: '#toolbar'
    });
    /**
     * 加载下拉列表信息
     *
     */
    queryRegionList();
    //绑定按钮事件
    $("#btn_risk_region_point_add").on("click",function(){
        addRegionPoint();
    });
    $("#btn_risk_region_point_edit").on("click",function(){
        editRegionPoint();
    });
    $("#btn_risk_region_point_delete").on("click",function(){
        deleteRegionPoint();
    });
    //查询按钮绑定事件
    $("#btn_search").on('click',function(){
        searchFun();
    });
    $('#btn_clear').on('click',function(){
        $('#searchForm1').form('clear');
       // $('#searchForm2').form('clear');
    });

});

/**
 * 打开风险点新增页面
 */
function addRegionPoint(){
    WindowManager.openLayer({
        type: 2,
        title: '新增风险点信息',
        area: ['800px', '50%'],
        content: ROOT_PATH + '/risk/region/point/add.shtml',
        callBack:function(data){//窗口关闭后的回调函数
            $('#listGrid').datagrid("reload");
            //alert(data);
        }
    });
           /* var index = top.layer.open({
                type: 2,
                title: '新增风险点信息',
                area: ['800px', '50%'],
                content: ROOT_PATH + '/risk/region/point/add.shtml',
                end:function(){
                    $('#listGrid').datagrid("reload");
                }
            });*/
}
/**
 * 打开风险点编辑页面
 */
function editRegionPoint(){
    var selectedRow =  $('#listGrid').datagrid('getSelected');
    if(selectedRow){
        var businessId = selectedRow.id;
        WindowManager.openLayer({
            type: 2,
            title: '编辑风险点信息',
            area: ['800px', '50%'],
            content: ROOT_PATH + '/risk/region/point/edit.shtml?businessId='+businessId,
            callBack:function(data){//窗口关闭后的回调函数
                $('#listGrid').datagrid("reload");
                //alert(data);
            }
        });
        /*var index = top.layer.open({
            type: 2,
            title: '编辑风险点信息',
            area: ['800px', '50%'],
            content: ROOT_PATH + '/risk/region/point/edit.shtml?businessId='+businessId,
            end:function(){
                $('#listGrid').datagrid("reload");
            }
        });*/
    }else{
        layer.alert("请选择数据！");
    }

}
/**
 * 删除风险点信息
 */
function deleteRegionPoint(){

    var selectedRow =  $('#listGrid').datagrid('getSelected');
    if(selectedRow) {
        var businessId = selectedRow.id;
        var index = layer.confirm("确定删除改行数据吗？",{btn:["确定","取消"]},function(){
            $.ajax({
                type:'post',
                url:ROOT_PATH + '/risk/region/deleteRegionPointInfo.shtml?businessId='+businessId,
                dataType:"json",
                success:function(result){
                    if (result.success){
                        layer.msg(result.message,{time:1000,icon:1},function(){
                            $('#listGrid').datagrid('reload');
                        });
                    }else{
                        layer.msg(result.message,{time:1000,icon:1},function(){
                        });
                    }
                }
            });
        })
    }else{
        layer.alert("请选择数据！");
    }
}
/**
 * 查询按钮方法
 */
function searchFun(){
    var search =$('#searchForm1').serializeJson();
    //var search2 =$('#searchForm2').serializeJson();
    //var search = $.extend(search1,search2);
    //排序参数
    search.sort = "addTime";
    search.order='desc';
    $('#listGrid').datagrid('load',search);
}
/**
 * 查询风险区域列表
 */
function queryRegionList(){
    $.ajax({
        url:ROOT_PATH + '/risk/region/queryComboboxList.shtml',//提交地址
        data:{},
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#search_regionId').combobox(
                {
                    data:result,
                    valueField:'id',
                    textField:'text',
                    loadFilter:function(data){
                     var newData = new Array();
                     newData.push({id:"",text:'请选择'});
                     for(var i=0;i<data.length;i++){
                     newData.push(data[i])
                     }
                     return newData;
                     },
                    /*onSelect:function (param) {
                        $('#regionId').val(param.id);
                    },
                    onChange:function(newVal,oldVal){
                        //区分选中还是手动输入
                        if(newVal.length !=32){
                            $('#regionId').val('');
                        }
                    }*/
                }
            );

        }
    });
}